//  service/article.js
"use strict";
const Service = require("egg").Service;
class ArticleService extends Service {
  // 创建文章
  async newArticle(data) {
    const { app } = this;
    try {
      const result = await app.mysql.insert("article", data);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 删除文章
  async deleteArticle(id) {
    const { app } = this;
    try {
      const result = await app.mysql.delete("article", { id: id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 更新文章 和 软删除
  async updateArticle(data) {
    const { app } = this;
    try {
      const result = await app.mysql.update("article", data);
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 文章列表
  async getArticleList(data) {
    const { app } = this;
    try {
      let sql = "SELECT * FROM article";
      const content = []; // 参数
      let isMore = false; // 是否有多个查询参数,只用于搜索查询
    /**
     * @模糊查询-量大的时候效率低
     * select * from user where name like ? % 内容 %
     * 在user表中全局查找name值 == 内容的
     * % 内容 % 全局查找内容
     *   内容 %  查找以 内容 开头的数据
     * % 内容  查找以 内容 结尾的数据
     * %内容_  倒数第二个字符为内容的数据
     * */
      // 搜索：id/名字
      if (data.keyword) {
        sql += " WHERE CONCAT(id,title) like ?";
        content.push("%" + data.keyword + "%");
        isMore = true;
      }
      // 状态
      if (data.status != "" && data.status != null && data.status != undefined) {
        if (isMore) {
          sql += "AND status == ?";
        } else {
          sql += " WHERE status == ?";
        }
        content.push(data.status);
        isMore = true;
      }
      // 允许评论
      if(data.allow_comment != "" && data.allow_comment != null && data.allow_comment != undefined){
        if (isMore) {
          sql += "AND allow_comment == ?";
        } else {
          sql += " WHERE allow_comment == ?";
        }
        content.push(data.allow_comment);
        isMore = true;
      }
      // 是否置顶
      if(data.topping != "" && data.topping != null && data.topping != undefined){
        if (isMore) {
          sql += "AND topping == ?";
        } else {
          sql += " WHERE topping == ?";
        }
        content.push(data.topping);
        isMore = true;
      }
      // 是否软删除
      if (data.deleted != "" && data.deleted != null && data.deleted != undefined) {
        if (isMore) {
          sql += "AND deleted == ?"; 
        } else {
          sql += " WHERE deleted == ?";
        }
        content.push(data.deleted);
        isMore = true;
      }
      // 创建时间
      if (data.begin && data.end) {
        if (isMore) {
          sql += "AND create_time BETWEEN ? AND ?";
        } else {
          sql += " WHERE create_time BETWEEN ? AND ?";
        }
        content.push(data.begin, data.end);
        isMore = true;
      }
      // 开启分页
      if (data.pageNo && data.pageSize) {
        const pageNo = data.pageNo; // 当前页码
        const pageSize = data.pageSize; // 一页展示多少条数据
        sql += " LIMIT ?,?";
        content.push(parseInt((pageNo - 1) * pageSize), parseInt(pageSize));
      }
      const res = {
        code: 200,
        page: data.pageNo,
        pageSize: data.pageSize,
        data: [],
        total: "",
      };
      res.data = await this.app.mysql.query(sql, content);
      if(data.deleted != 1){
        let result = await this.app.mysql.query("SELECT COUNT(*) as total FROM article WHERE deleted != 1 OR deleted IS NULL");
        res.total = result[0].total
      }else{
        res.total = await this.app.mysql.count("article");
      }
      return res;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  // 文章详情
  async getArticleDetail(id) {
    const { app } = this;
    try {
      const result = await app.mysql.get("article", { id: id });
      return result;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}
module.exports = ArticleService;
